package sn.gandal.agriculture.agrimobile.j2me.gui;

import java.io.IOException;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.StringItem;
import javax.microedition.midlet.MIDlet;
import javax.microedition.rms.RecordStoreException;
import sn.gandal.agriculture.agrimobile.j2me.businessObjects.BusinessServicesImpl;
import sn.gandal.agriculture.agrimobile.j2me.businessObjects.IBusinessServices;

/**
 *
 * @author dysow
 */
public class InformationForm extends Form implements CommandListener
{

    private Display display;
    private Displayable previousScreen;
    private StringItem strInformation = null;
    private Command cmdOk;

    public InformationForm(Display display, Displayable previous)
    {
	super("Information");

	this.display = display;
	this.previousScreen = previous;

	this.initialiseComponents();
	this.initialiseCommands();
	this.addComponents();
	this.addCommands();

	this.setCommandListener(this);
    }

    private void initialiseComponents()
    {
	this.strInformation = new StringItem("", "");
    }

    private void initialiseCommands()
    {
	this.cmdOk = new Command("Ok", Command.OK, 1);
    }

    private void addComponents()
    {
	this.append(this.strInformation);
    }

    private void addCommands()
    {
	this.addCommand(this.cmdOk);
    }

    public void commandAction(Command c, Displayable d)
    {
	if (c.equals(this.cmdOk))
	{
	    this.okAction();
	}
    }

    private void okAction()
    {
        IBusinessServices service=BusinessServicesImpl.getInstance();
        boolean load = false;
        try {
            load = service.isAbonneLoaded();
        } catch (RecordStoreException ex) {
            ex.printStackTrace();
        }
    if(load){
	this.display.setCurrent(this.previousScreen);
    }else{
            MIDlet midlet=MainMenu.getMidlet();
            this.display.setCurrent(new GestionTransmissionMenu(display, new IdentificationForm(midlet, display), midlet));
        }
    }

    public void setMessage(String message)
    {
	this.strInformation.setText(message);
    }
}
